/* jshint esversion: 6 */
var fs = require("fs");
var fd = fs.openSync('./from.js', 'w');

let myData =
  [
    [
      {
        id: 'student',
        name: '驾校学员',
        type: 'Input',
        attr: {
          message: '请输入学员姓名',
          placeolder: '请输入学员姓名'
        }
      },
      {
        id: 'apply_type',
        name: '申请类型',
        type: 'Select',
        attr: {
          message: '请选择申请类型',
          placeolder: '请选择申请类型',
          options: [
            {
            val: '1',
            name: '初次申请'
          },
          {
            val: '2',
            name: '增加准驾车型'
          },
          {
            val: '3',
            name: '持军警驾驶证'
          },
          {
            val: '4',
            name: '持境外驾驶证'
          }
        ]
        }  
      },
      {
        id: 'student',
        name: '原驾车型',
        type: 'Input',
        attr: {
          message: '请输入原驾车型',
          placeolder: '请输入原驾车型'
        }
      },
    ],
    [
      {
        id: 'course_type',
        name: '班别名称',
        type: 'Input',
        attr: {
          message: '请输入班别名称',
          placeolder: '请输入班别名称'
        }
      },
      {
        id: 'course_price',
        name: '班别名称',
        type: 'InputNumber',
        attr: {
          message: '请输入班别价格',
          placeolder: '请输入班别价格'
        }
      },
      {
        id: 'course_type',
        name: '班别车型',
        type: 'InputNumber',
        attr: {
          message: '请输入班别车型',
          placeolder: '请输入班别车型'
        }
      },
    ],
    [
      {
        id: 'records',
        name: '修改记录',
        type: 'TextArea',
        attr: {
          message: '请输入修改记录',
          rows: '9'
        }
      }
    ]
  ];

// var layNum = [1,1]


var layout = function (len) {
  if (len == 1) {
    return (["lg={8} md={12} sm={24}"]);
  }
  if (len == 2) {
    return (["lg={8} md={12} sm={24}", "lg={{span: 8, offset: 4}} md={12} sm={24}"]);
  }
  if (len == 3) {
    return (["lg={6} md={12} sm={24}", "lg={{ span: 6, offset: 2 }} md={12} sm={24}", "lg={{ span: 6, offset: 2 }} md={12} sm={24}"]);
  }
};

var fromItem = {
  Input(id, attr) {
    return (`{getFieldDecorator('${id}', {
          rules: [{ required: ${attr.required || false}, message: '${attr.message || '请输入'}' }],
        })(
        <Input placeholder='${ attr.placeolder || '请输入'}' />
        )}`);
  },
  Select(id, attr) {
    var opts = '';
    attr.options.forEach(ele => {
      opts = opts + `<Option value="${ele.val}">${ele.name}</Option>`;
    });
    return (`{getFieldDecorator('${id}', {
          rules: [{ required: ${attr.required || false}, message: '${attr.message || '请输入'}' }],
        })(
          <Select placeholder="${ attr.placeolder || '请输入'}">
            ${opts}
          </Select>
          )}`);
  },
  CheckBox(id, attr) {
    return (`{getFieldDecorator(’${id}‘, {
        valuePropName: ${attr.valuePropName},
        initialValue: ${attr.initialValue},
      })(
        <Checkbox>${attr.txt}</Checkbox>
      )}`);
  },
  DatePicker(id, attr) {
    return (`{getFieldDecorator('${id}', {
        rules: [{ required: ${attr.required || false}, message: '${attr.message || '请输入'}' }],
      })(
        <DatePicker
          placeholder="${ attr.placeolder || '请输入'}"
          style={{ width: '100%' }}
          getPopupContainer={trigger => trigger.parentNode}
        />
      )}`);
  },
  TextArea(id, attr) {
    return (`
      {getFieldDecorator('${id}', {
        rules: [{ required: ${attr.required || false}, message: '${attr.message || '请输入'}' }],
      })(
        <TextArea rows={${attr.rows || 4}} />)
      }`);
  },
  InputNumber(id, attr) {
    return (`
    {getFieldDecorator('${id}', {
      rules: [{ required: ${attr.required || false}, message: '${attr.message || '请输入'}' }],
    })(
    <InputNumber style={{ width: '100%' }} min={${attr.min || 0}} max={${attr.max || 10000}} step={${attr.step || 1}} />)
    }`);
  }
};

var fromData = '';
myData.map((supitem, ind) => {
  fs.writeSync(fd, `
  <Row gutter={16}>`, null, null);
  supitem.map((item, index) => {
    fromData = `
          <Col ${layout(myData[ind].length)[index]}>
            <Form.Item label={fieldLabels['${item.id}']}>
                ${fromItem[item.type || 'Input'](item.id, item.attr)}
            </Form.Item>
          </Col>` ;
    fs.writeSync(fd, fromData, null, null);
  });
  fs.writeSync(fd, `
  </Row>`, null, null);
});


/*
*以异步的方式向文件中写入内容
*/

var eleList = ''
myData.map((supitem) => {
  supitem.map(item => {
  eleList = eleList + `
  '${item.id}':' ${item.name}',`
    ;
})
})

fs.writeSync(fd, eleList, null, null);


fs.closeSync(fd);
